package com.ricohgag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ricohgag.entity.Goods;
import com.ricohgag.entity.custom.GoodsTools;
import com.ricohgag.mapper.GoodsMapper;
import com.ricohgag.service.GoodsService;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public IPage<Goods> selectGoodsPage(String name, Page<Goods> page) throws Exception{
        QueryWrapper<Goods> qw = new QueryWrapper<>();

        //查询like name的所有id。

        if(name != null){
            qw.like("goods_name", name);
        }

        IPage<Goods> shopAdIPage = goodsMapper.selectPage(page, qw);
        Assert.assertSame(page, shopAdIPage);

        return shopAdIPage;
    }

    @Override
    public IPage<Goods> selectHomeGoodsPage(Page<Goods> page, GoodsTools goodsTools) throws Exception{
        QueryWrapper<Goods> qw = new QueryWrapper<>();
            qw.like("goods_name", goodsTools.getName());
        //这是种类选择
        if(goodsTools.getCatalog()!=-1){
            qw.eq("catalog_id", goodsTools.getCatalog());
        }
        //这是价格选择区间
        int priceD = goodsTools.getPriceD();
        if(priceD!=-1){
            qw.between("sell_price", goodsTools.getPrice()[priceD], goodsTools.getPrice()[priceD+1]);
        }
        //排序方式
        String sortStr = goodsTools.getOrderStr()[goodsTools.getSort()];
        boolean falg = sortStr.endsWith("asc");
        if(falg){
            qw.orderByAsc(sortStr.substring(0, sortStr.lastIndexOf(" ")));
        } else {
            qw.orderByDesc(sortStr);
        }
        System.err.println("test::: "+sortStr);

        IPage<Goods> adminUserIPage = goodsMapper.selectPage(page, qw);
        Assert.assertSame(page, adminUserIPage);

        return adminUserIPage;
    }

    @Override
    public Goods selectGoodsOne(Integer id) throws Exception{

        return goodsMapper.selectById(id);
    }

    @Override
    public int updateGoods(Goods goods) throws Exception{
        return goodsMapper.updateById(goods);
    }

    @Override
    public int insertGoods(Goods goods) throws Exception{
        return goodsMapper.insert(goods);
    }

    @Override
    public int deleteGoods(int id) throws Exception{
        return goodsMapper.deleteById(id);
    }

    @Override
    public List<Goods> selectGoodsHot() throws Exception{
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        qw.eq("is_hot", 1);
        List<Goods> goodsList = goodsMapper.selectList(qw);

        return goodsList;
    }

    @Override
    public List<Goods> selectGoodsNew() throws Exception{
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        qw.eq("is_new", 1);
        List<Goods> newList = goodsMapper.selectList(qw);

        return newList;
    }

    @Override
    public List<Goods> selectGoodsByCatalogId(int catalogId){
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        qw.eq("catalog_id", catalogId);

        return goodsMapper.selectList(qw);
    }





}
